package com.tim.helper.file;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 修改yml配置参数
 */
@Slf4j
public class YmlChangeHelper {

	//idea provider目录
	String dir = "D:/IntelliJ IDEA/workSpace/qft-all/qft-provider";

	//eclipse provider目录
//	String dir = "D:/qftGit/qft-all/qft-provider";
	
	@Test
	public void back2dev() {
		Map<String, String> propertyMap = new HashMap<>();		
		propertyMap.put("/qft_basics", " jdbc:mysql://192.168.50.170:3306/qft_basics_dev?serverTimezone=GMT%2B8&allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false");
		propertyMap.put("/qft_lease", " jdbc:mysql://192.168.50.170:3306/qft_lease_dev?serverTimezone=GMT%2B8&allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false");
		propertyMap.put("/qft_finance", " jdbc:mysql://192.168.50.170:3306/qft_finance_dev?serverTimezone=GMT%2B8&allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false");
		propertyMap.put("username:", "root");
		propertyMap.put("password:", "chen@1123");
		propertyMap.put("namespace:", "3ba34dc4-1d0a-4692-8f19-fd249d063c41");

		doChange(propertyMap);
	}

	@Test
	public void dev2Test2() {
		Map<String, String> propertyMap = new HashMap<>();		
		propertyMap.put("/qft_basics", " jdbc:mysql://rm-m5eh84g28g1b1fee7go.mysql.rds.aliyuncs.com:3306/qft_basics_cq?serverTimezone=GMT%2B8&allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false");
		propertyMap.put("/qft_lease", " jdbc:mysql://rm-m5eh84g28g1b1fee7go.mysql.rds.aliyuncs.com:3306/qft_lease_renew?serverTimezone=GMT%2B8&allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false");
		propertyMap.put("/qft_finance", " jdbc:mysql://rm-m5eh84g28g1b1fee7go.mysql.rds.aliyuncs.com:3306/qft_finance_renew?serverTimezone=GMT%2B8&allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false");
		propertyMap.put("username:", "qftcqall");
		propertyMap.put("password:", "cl3vSeuVEPgU");
		propertyMap.put("namespace:", "bfda01c2-8202-4af7-8fe8-430326ba9d44");

		doChange(propertyMap);
	}
	
	@Test
	public void dev2Release() {
		Map<String, String> propertyMap = new HashMap<>();		
		propertyMap.put("/qft_basics", " jdbc:mysql://rm-m5eh84g28g1b1fee7go.mysql.rds.aliyuncs.com:3306/qft_basics?serverTimezone=GMT%2B8&allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false");
		propertyMap.put("/qft_lease", " jdbc:mysql://rm-m5eh84g28g1b1fee7go.mysql.rds.aliyuncs.com:3306/qft_lease?serverTimezone=GMT%2B8&allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false");
		propertyMap.put("/qft_finance", " jdbc:mysql://rm-m5eh84g28g1b1fee7go.mysql.rds.aliyuncs.com:3306/qft_finance?serverTimezone=GMT%2B8&allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false");
		propertyMap.put("username:", "qftnewall   ");
		propertyMap.put("password:", "64zp6ej5tCBmnS");
		
		doChange(propertyMap);
	}
	
	@Test
	public void devTo245() {
		Map<String, String> propertyMap = new HashMap<>();		
		propertyMap.put("/qft_basics", " jdbc:mysql://192.168.50.245:3306/qft_basics?serverTimezone=GMT%2B8&allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false");
		propertyMap.put("/qft_lease", " jdbc:mysql://192.168.50.245:3306/qft_lease?serverTimezone=GMT%2B8&allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false");
		propertyMap.put("/qft_finance", " jdbc:mysql://192.168.50.245:3306/qft_finance?serverTimezone=GMT%2B8&allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false");
		propertyMap.put("username:", "qft_qft");
		propertyMap.put("password:", "qft!@#1123");
		propertyMap.put("namespace:", "77b79f03-17f9-4570-8c8d-8c7838b681d3");

		doChange(propertyMap);
	}
	
	private void doChange1(Map<String, String> propertyMap) {
		try {
			Files.walkFileTree(Paths.get(dir), new SimpleFileVisitor<Path>() {
				@Override
				public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
					String fileName = file.toString();
					if(fileName.endsWith("-dev.yml") || fileName.endsWith("application.yml")) {
						System.out.println(fileName);
						List<String> list = Files.readAllLines(file);
						int flag = 0;
						if(fileName.endsWith("-dev.yml")) {
							flag = 1;
						}
						for (int i = 0; i < list.size(); i++) {
							String str = list.get(i);
							String tempStr = str.trim();
							if(flag==0 && tempStr.equals("profiles: dev")) {
								//标记开始
								flag = 1;
								continue;
							}else if(flag == 1) {
								if(tempStr.equals("redis:") || tempStr.equals("rabbitmq:") || tempStr.equals("mongodb:")) {
									//遇到redis、rabbitmq配置时；开始回写
									Files.write(file, list);
									return super.visitFile(file, attrs);
								}else {
									//判定是否包含关键词，替换
									for(String key : propertyMap.keySet()) {
										if(str.contains(key)) {
											//修改值
											String prefix = str.substring(0, str.indexOf(":")+1);
											prefix = prefix + " ";//拼接空格
											list.set(i, prefix+propertyMap.get(key));
										}
									}
								}
							}
						}
					}
					return super.visitFile(file, attrs);
				}
			});
		} catch (IOException e) {
			e.printStackTrace();
		}
	}


	private void doChange(Map<String, String> propertyMap) {
		try {
			Files.walkFileTree(Paths.get(dir), new SimpleFileVisitor<Path>() {
				@Override
				public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
					String fileName = file.toString();
					if(fileName.endsWith("-dev.yml") || fileName.endsWith("application.yml")) {
						System.out.println(fileName);
						List<String> list = Files.readAllLines(file);
						int flag = 0;
						if(fileName.endsWith("-dev.yml")) {
							flag = 1;
						}
						for (int i = 0; i < list.size(); i++) {
							String str = list.get(i);
							String tempStr = str.trim();
							if(flag==0 && tempStr.equals("nacos:")) {
								//标记开始
								flag = 1;
								continue;
							}else if(flag == 1) {
								if(tempStr.equals("redis:") || tempStr.equals("rabbitmq:")
										|| tempStr.equals("mongodb:") || tempStr.equals(str.replaceAll("^\\s+", ""))) {
									//遇到redis、rabbitmq配置时；开始回写
									break;
								}else {
									//判定是否包含关键词，替换
									for(String key : propertyMap.keySet()) {
										if(str.contains(key)) {
											//修改值
											String prefix = str.substring(0, str.indexOf(":")+1);
											prefix = prefix + " ";//拼接空格
											list.set(i, prefix+propertyMap.get(key));
										}
									}
								}
							}
						}
						if(flag == 1) {
							Files.write(file, list);
							return super.visitFile(file, attrs);
						}
					}
					return super.visitFile(file, attrs);
				}
			});
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
